import { Component, Injector, ViewChild, ViewEncapsulation } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { AppConsts } from '@shared/AppConsts';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { HttpClient } from '@angular/common/http';
import { ProfileServiceProxy, DataItemServiceProxy, DataItemDetailModel } from '@shared/service-proxies/service-proxies';
import { FileDownloadService } from '@shared/utils/file-download.service';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { Paginator } from 'primeng/components/paginator/paginator';
import { Table } from 'primeng/components/table/table';
import { DataItemDetailCreateComponent } from '../BaseDataItem/DataItemDetailCreate/DataItemDetailCreate.component';
import { DataItemCreateComponent } from '../BaseDataItemType/DataItemCreate/DataItemCreate.component';
import { finalize } from 'rxjs/operators';
import { from } from 'rxjs';
import * as _ from 'lodash';
import { TreeNode, MenuItem } from 'primeng/api';

@Component({
  selector: 'BaseDataItem',
  templateUrl: './BaseDataItem.component.html',
  styleUrls: ['./BaseDataItem.component.css']
  
})
export class BaseDataItemComponent extends AppComponentBase {
  @ViewChild('DataItemDetailCreate') DataItemDetailCreate: DataItemDetailCreateComponent;
  @ViewChild('DataItemCreate') DataItemCreate: DataItemCreateComponent;
  @ViewChild('dataTable') dataTable: Table;
  @ViewChild('paginator') paginator: Paginator;
  itemAyy = [];
  ouContextMenuItems = [];
  selectedOu: TreeNode;
  filterText = '';
  itemId: string;
  constructor(injector: Injector,
    private _dataItemAppServiceProxy: DataItemServiceProxy,
    private _activatedRoute: ActivatedRoute,
    private _httpClient: HttpClient) {
    super(injector);
    this.getDataItemTree();
  }
  getDataItemTree(): void {
    this._dataItemAppServiceProxy.getDataItemTreeList().subscribe(resultdata => {
      console.log(resultdata)
      this.itemAyy = resultdata;
      //this.selectedItem = this.itemAyy[0].value;
      this.ouContextMenuItems = [
        {
          label: '编辑',
          command: (event) => {
            this.DataItemCreate.show(this.selectedOu.data);
          }
        },
        {
          label: '删除',
          command: () => {
            console.log(this.selectedOu);
            console.log(this.selectedOu.data);
            this.message.confirm(
              this.l('UserDeleteWarningMessage', this.selectedOu.label),
              this.l('AreYouSure'),
              isConfirmed => {
                if (isConfirmed) {
                  this._dataItemAppServiceProxy
                    .deleteDataItem(this.selectedOu.data)
                    .subscribe(() => {
                      this.getDataItemTree();
                      this.notify.success(this.l('SuccessfullyDeleted'));
                    });
                }
              }
            );
          }
        }
      ];
    });
  }
  createDataItemDetail(): void {
    this.DataItemDetailCreate.show(undefined, this.itemId);
  }
  createDataItem(): void {
    this.DataItemCreate.show();
  }
  getDataItemDetail(event?: LazyLoadEvent) {
    if (this.primengTableHelper.shouldResetPaging(event)) {
      this.paginator.changePage(0);
      return;
    }
    this.primengTableHelper.showLoadingIndicator();

    this._dataItemAppServiceProxy.getDataItemDetailList(
      this.filterText,
      this.itemId,
      'creationTime',
      //this.primengTableHelper.getSorting(this.dataTable),
      this.primengTableHelper.getMaxResultCount(
        this.paginator,
        event
      ),
      this.primengTableHelper.getSkipCount(this.paginator, event)
    )
      .pipe(
        finalize(() => this.primengTableHelper.hideLoadingIndicator())
      )
      .subscribe(result => {
        this.primengTableHelper.totalRecordsCount = result.totalCount;
        this.primengTableHelper.records = result.items;
        this.primengTableHelper.hideLoadingIndicator();
        _.forEach(result.items, viewItem => {
          if (viewItem.itemId !== '') {
            this._dataItemAppServiceProxy.getDataItemForEdit(viewItem.itemId)
              .subscribe(dataItem => {
                viewItem.dataItem = dataItem.itemName;
              });
          } else {
            viewItem.dataItem = '-';
          }
        });
      });
  }
  reloadPage(): void {
    this.paginator.changePage(this.paginator.getPage());
  }

  deleteDateItemDetail(dataItemDetail: DataItemDetailModel): void {
    console.log(dataItemDetail);
    this.message.confirm(
      this.l('UserDeleteWarningMessage', dataItemDetail.itemName),
      this.l('AreYouSure'),
      isConfirmed => {
        if (isConfirmed) {
          this._dataItemAppServiceProxy.deleteDataItemDetail(dataItemDetail.id)
            .subscribe(() => {
              this.reloadPage();
              this.notify.success(this.l('SuccessfullyDeleted'));
            });
        }
      }
    );
  }

  nodeSelect(event) {
    this.itemId = event.node.data;
    if (this.primengTableHelper.shouldResetPaging(event)) {
      this.paginator.changePage(0);
      return;
    }
    this.primengTableHelper.showLoadingIndicator();

    this._dataItemAppServiceProxy.getDataItemDetailList(
      this.filterText,
      this.itemId,
      'creationTime',
      //this.primengTableHelper.getSorting(this.dataTable),
      this.primengTableHelper.getMaxResultCount(this.paginator, event),
      this.primengTableHelper.getSkipCount(this.paginator, event)
    )
      .pipe(
        finalize(() => this.primengTableHelper.hideLoadingIndicator())
      )
      .subscribe(result => {
        this.primengTableHelper.totalRecordsCount = result.totalCount;
        this.primengTableHelper.records = result.items;
        this.primengTableHelper.hideLoadingIndicator();
        _.forEach(result.items, viewItem => {
          if (viewItem.itemId !== '') {
            this._dataItemAppServiceProxy.getDataItemForEdit(viewItem.itemId)
              .subscribe(dataItem => {
                viewItem.dataItem = dataItem.itemName;
              });
          } else {
            viewItem.dataItem = '-';
          }
        });
      });
  }


}
